package com.interview.service.impl;

import com.interview.common.Result;
import com.interview.entity.ExameeDrawLots;
import com.interview.service.DataUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataUpLoadServiceImpl implements DataUpLoadService {
    @Autowired
    private Environment environment;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public boolean upLoadAbsent(Map<String, String> absentSequences) {
        //配置的数据管理服务IP
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataAggregate/absentSequence";

        ParameterizedTypeReference<Result> responseType = new ParameterizedTypeReference<Result>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();

        absentSequences.forEach((key, value) -> {
            map.add(key, value);
        });
        // 组装请求体
        //发起请求(数据解析)
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<Result> response = restTemplate.exchange(targetURL,
                HttpMethod.POST,
                request,
                responseType);
        Result result = response.getBody();
        return result.getFlag();

    }

    @Override
    public boolean upLoadDrawLots(List<ExameeDrawLots> exameeDrawLotsList) {
        //配置的数据管理服务IP
        String dataCenterURL = environment.getProperty("DataCenterURL");
        String targetURL = dataCenterURL+"/dataAggregate/absentSequence";

        ParameterizedTypeReference<Result> responseType = new ParameterizedTypeReference<Result>(){};
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        return true;
    }
}
